package com.vodone.v1pin.core.utils
{
	import flash.text.TextLineMetrics;
	
	import mx.controls.Label;
	
	public class TextUtil
	{
		public static const userNameChat:String = "abcdefghijklmnopqrstuvwxyz0123456789_";
		/**
		 * 比较字符是否合法 
		 * @param source
		 * @return 
		 * 
		 */		
		public static function compareChat( source:String ):Boolean
		{
			var len:int = source.length;
			for( var i:uint = 0;i<len;i++ ){
				if( userNameChat.indexOf(String(source.charAt(i)).toLowerCase()) != -1 || String(source.charAt(i)).charCodeAt() > 5000){
					return true;
				}
			}
			return false;
		}
		/**
		 * 删除html标签
		 */			
		public static function removeTags( source:String ):String
		{
			if( null != source )
			{
				var reg:RegExp = new RegExp("<[^>]*>","g");
				return source.replace( reg , "" );
			}
			return source;
		}
		/**
		 * 获取固定长度的随机字符串
		 */
		public static function getRandomStr(len:uint):String
		{
			var temp:String = "";
			for(var i :uint = 0 ; i < len ; i ++)
			{
				temp += getRandomChar();
			}
			return temp;
		}
		public static function getRandomChar():String
		{
			var index:uint = (Math.random() * (firstLetter.length - 1));
			return firstLetter[index];
		}
		
		/**
		 * 存放国标一级汉字不同读音的起始区位码对应读? 
		 */		
		private static const firstLetter : Array = [
			'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J',
			'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
			'T', 'W', 'X', 'Y', 'Z'];
		private static const doubleLetters : Array = [
			'Ａ', 'Ｂ', 'Ｃ', 'Ｄ', 'Ｅ', 'Ｆ', 'Ｇ', 'Ｈ', 'Ｊ',
			'Ｋ', 'Ｌ', 'Ｍ', 'Ｎ', 'Ｏ', 'Ｐ', 'Ｑ', 'Ｒ', 'Ｓ',
			'Ｔ', 'Ｗ', 'Ｘ', 'Ｙ', 'Ｚ'
		];
		
		
		
		public static function getNumberFormat(num:Number,count:uint):Number
		{
			//参数为小数点后的数量
			var size:uint = Math.pow(10,count);
			return ( num * size + 1 ) / size;
		}
		
		/**
		* <p>Title:获得汉字的拼音首字母</p>
		* <p>Description: GB 2312-80 把收录的汉字分成两级。第一级汉字是常用汉字，计 3755 个，
		* 置于 16?5 区，按汉语拼音字母／笔形顺序排列；第二级汉字是次常用汉字，
		* 记3008 个，置于 56?7 区，按部首／笔画顺序排列。本程序启用了字典查询，共能?0902?
		* 汉字的声母。同时对符合声母（zh，ch，sh）只能取首字母（z，c，s?</p>
		* <p>Copyright: Copyright (c) 2008</p>
		* <p>Company: </p>
		* @author JINXIANGYANG
		* @version 0.1
		*/
		
		public static const IDENTICAL_MODE : int = 0;
		
		public static const STR1SHORT_MODE : int = 1;
		
		public static const STR2SHORT_MODE : int = 2;
		
		public static const ANYSHORT_MODE : int = 3;

		/**
		 * 替换html标签
		 */
		public static function HtmlDecode(text:String):String
		{
			text = StringReplaceAll(text, "&quot;",'"');
			text = StringReplaceAll(text, "&apos;","'");
			text = StringReplaceAll(text, "&amp;","&");
			text = StringReplaceAll(text, "&lt;","<");
			text = StringReplaceAll(text, "&gt;",">");
			text = StringReplaceAll(text, "&ldquo;","“");
			text = StringReplaceAll(text, "&rdquo;","”");
			text = StringReplaceAll(text, "&#034;",'"');
			text = StringReplaceAll(text, "&#039;","'");
			return text;
		}
		
		/**
		 *替换所有需要替换的字符 
		 * @param source
		 * @param find
		 * @param replacement
		 * @return 
		 * 
		 */		
		public static function StringReplaceAll( source:String, find:String = " ", replacement:String = "" ):String
		{   
  		  return source.split( find ).join( replacement );   
 		}
		
		
		public static function getTextWidth(str:String,fontSize:int):int
		{
			var label:Label = new Label();
			label.setStyle("fontSize",fontSize);
			label.regenerateStyleCache(false);
			var ma:TextLineMetrics = label.measureText(str);
			return ma.width+5;
		}
		
		/**
		 * @param date YYYYMMDD格式的整数
		 * @param minute 默认的分钟
		 * @param second 默认的秒数
		 * @return 1970.1.1日起的毫秒数
		 */			
		public static function getTimeFromYYYYMMDD( time:Number ):String
		{
			var date:Date = new Date(time);
			var mo:String = (date.month + 1<10)?("0"+(date.month + 1)):String(date.month + 1);
			var day:String = (date.date<10)?("0"+date.date):String(date.date);
			return date.fullYear + "-" + mo + "-" + day;

		}

		
	}
}